package util

import (
	"fmt"
	"gin_cli/conf"

	jwt "github.com/jwt-go-master"
)

/*
{ data: { id: 68, type: 'wechat', social: 1 },
  iat: 1536286841,
  exp: 1536373241,
  nonceStr: 'S2F603MEkNiXzSmV441jlySkOLrOUfSU' }
*/

var jwtSecret = []byte(conf.AppConf.JwtSecret)

func GenerateToken() {
	data := map[string]interface{}{
		"id":     68,
		"type":   "wechat",
		"social": 1,
	}
	token := jwt.New(jwt.SigningMethodHS256)
	claims := make(jwt.MapClaims)
	claims["data"] = data
	claims["exp"] = 1536373241
	claims["iat"] = 1536286841
	claims["nonceStr"] = "S2F603MEkNiXzSmV441jlySkOLrOUfSU"
	token.Claims = claims
	s, err := token.SignedString(jwtSecret)
	if err != nil {
		fmt.Println("err")
		fmt.Println(err)
	}
	fmt.Println(s)
}

func ParseToken(token string) (map[string]interface{}, error) {
	result, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
		return jwtSecret, nil
	})
	if err != nil {
		return map[string]interface{}{}, err
	}
	claims := result.Claims.(jwt.MapClaims)
	return claims, nil
}
